package com.simple.datax.transform.tcs;

import net.easipay.ec.v2.ECDataType;
import net.easipay.ec.v2.ECFlow201ResponseDocument;
import net.easipay.ec.v2.ECFlow201ResponseDocument.ECFlow201Response;
import net.easipay.ec.v2.ECFlow201ResponseDocument.ECFlow201Response.ResponseHead;
import cn.gov.chinaport.tcs.flow.TcsFlow201ResponseDocument;
import cn.gov.chinaport.tcs.flow.TcsFlow201ResponseDocument.TcsFlow201Response;

import com.simple.datax.api.TransformerException;
import com.simple.datax.transform.util.TransformUtils;

/**
 * @Title: Tcs2EcFlowResponseTransformer.java
 * @Package com.simple.datax.transform.tcs
 * @Description: TODO(添加描述)
 * @author Paladin Chen
 * @date 2013-1-9 下午4:08:09
 * @version V1.0
 */
public class Tcs2EcFlowResponseTransformer {
	public ECFlow201ResponseDocument transform(TcsFlow201ResponseDocument xmlDoc) throws TransformerException {
		ECFlow201ResponseDocument ecFlowResDoc = ECFlow201ResponseDocument.Factory.newInstance();
		ECFlow201Response ecFlowRes = ecFlowResDoc.addNewECFlow201Response();
		ResponseHead ecResponseHead = ecFlowRes.addNewResponseHead();
		TcsFlow201Response.ResponseHead tcsResponseHead = xmlDoc.getTcsFlow201Response().getResponseHead();

		ecResponseHead.setRequestMessageId(TransformUtils.changeNullToBlank(tcsResponseHead.getRequestMessageId()));
		ecResponseHead.setMessageId(TransformUtils.changeNullToBlank(tcsResponseHead.getMessageId()));
		ecResponseHead.setTaskId(TransformUtils.changeNullToBlank(tcsResponseHead.getTaskId()));
		ecResponseHead.setCorpTaskId(TransformUtils.changeNullToBlank(tcsResponseHead.getCorpTaskId()));
		ecResponseHead.setResultCode(TransformUtils.changeNullToBlank(tcsResponseHead.getResultCode()));
		ecResponseHead.setResultNote(TransformUtils.changeNullToBlank(tcsResponseHead.getResultNote()));
		ecResponseHead.setResultValue(TransformUtils.changeNullToBlank(tcsResponseHead.getResultValue()));
		ecResponseHead.setBpNo(TransformUtils.changeNullToBlank(tcsResponseHead.getBpNo()));

		if (tcsResponseHead.getResultMessage() != null) {
			try {
				ecResponseHead
						.setResultMessage(ECDataType.Factory.parse(tcsResponseHead.getResultMessage().toString()));
			} catch (Exception e) {
				throw new TransformerException("Parse Flow Response Data Error.", e);
			}
		}

		if (xmlDoc.getTcsFlow201Response().getResponseList() != null) {
			TcsFlow201Response.ResponseList.ActionResult tcsars[] = xmlDoc.getTcsFlow201Response().getResponseList()
					.getActionResultArray();
			ECFlow201Response.ResponseList ecResponseList = ecFlowRes.addNewResponseList();

			for (TcsFlow201Response.ResponseList.ActionResult tcsActionResult : tcsars) {
				ECFlow201Response.ResponseList.ActionResult ecActionResult = ecResponseList.addNewActionResult();
				ecActionResult.setActionId(TransformUtils.changeNullToBlank(tcsActionResult.getActionId()));
				ecActionResult.setResultCode(TransformUtils.changeNullToBlank(tcsActionResult.getResultCode()));
				ecActionResult.setResultValue(TransformUtils.changeNullToBlank(tcsActionResult.getResultValue()));
			}

		}
		return ecFlowResDoc;
	}
}
